/**
 *	Contains 'Rules' / 'Win conditions' for the 'The Swapping Game' game type
**/

"use strict"


/**
 *	Add game rules
**/
GameLogic.validator.addRules({
	
	// enforces that the origin square is full (contains a piece to move) (kind of useless considering below, but useful to inform the user)
	checkOriginSquareFull: function(game, move) {
		if ( game.state[move.y][move.x] === 0 ) {
			return {
				success: false,
				message: "origin square is empty! you must move a piece to continue"
			}
		}
		return { success: true }
	},
	
	// enforces that the origin square belongs to the player
	checkOriginSquareContainsPlayersPiece: function(game, move) {
		if ( game.state[move.y][move.x] !== game.turn ) {
			return {
				success: false,
				message: "origin square does not contain your piece"
			}
		}
		return { success: true }		
	},
	
	
	// the destination square must be specified, and it must be different from the origin square
	checkNewDestinationSquareSpecified: function(game, move) {
		
		// check if the destination actually exists
		if ( typeof move.x2 !== "number" || typeof move.y2 !== "number" ) {
			return {
				success: false,
				message: "you didn't specify what square this piece should move to"
			}
		
		// make sure the destination and the origin are not the same place
		} else if ( move.x === move.x2 && move.y === move.y2 ) {
			return {
				success: false,
				message: "the destination square and origin square are the same place"
			}
		}
		
		return { success: true }
	},
	
	
	// enforces that the destination square is empty (for now!)
	checkDestinationSquareEmpty: function(game, move) {
		if ( game.state[move.y2][move.x2] !== 0 ) {
			return {
				success: false,
				message: "destination square must be empty to move into it!"
			}
		}
		return { success: true }
	},
	
	// up, down, left, right - no diagonals
	checkMovedOrthogonally: function(game, move) {
		if ( move.x === move.x2 || move.y === move.y2) {
			return { success: true }
		}
		return {
			success: false,
			message: "move was not orthogonal"
		}
	},
	
	// invoke a meta-rule here
	checkMoveDistanceIs1: function(game, move) {
		return this.checkMoveDistanceIsX(game, move, 1)
	},
	
	// enforces a move distance of X in horizontal / vertical / diagonal
	checkMoveDistanceIsX: function(game, move, distance) {
		var diffx = move.x - move.x2,
			magx = Math.sqrt(diffx * diffx),
			diffy = move.y - move.y2,
			magy = Math.sqrt(diffy * diffy)
		
		// true if x or y or both == distance
		if ( magx === distance || magy === distance ) {
			return { success: true }
		}

		return {
			success: false,
			message: "you did not move the required distance of " + distance
		}
	}
	
})



/**
 *	Add win conditions
**/
GameLogic.winChecker.addRules({
	
	// player 1 wins if he moves all his pieces to the top
	checkBottomLinePlayer1: function(game) {
		var bottom = game.board.h - 1
		for (var i=0; i<game.board.w; i++) {
			if ( game.state[bottom][i] !== 1 ) {		//check the entire top line
				return 0								//must not all be player 1, so return 0
			}
		}
		return 1
	},
	
	// player 2 wins if he moves all his pieces to the bottom
	checkTopLinePlayer2: function(game) {
		for (var i=0; i<game.board.w; i++) {
			if ( game.state[0][i] !== 2 ) {				//check the entire bottom line
				return 0								//must not all be player 2, so return 0
			}
		}
		return 2
	}
	
})
